package com.moli.iov.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author fudh
 * @ClassNmme ExcelUtils
 * @date 2019/1/28 11:24
 * @Description: TODO
 */
public class ExcelUtils {

    private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class);

    /**
     * 从excel中读取TBOX数据
     *
     * @return
     */
    public static void getTBoxDataFromExcel() {

        File file = new File("D:\\车辆汇总.xlsx");
        if (!file.exists()) {
            return;
        }
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);//获取第一个sheet
            //获取第一页的vin
            getVinFromSheet(sheet);
            Sheet sheet1 = workbook.getSheetAt(1);//获取第一个sheet
            //获取第二页的vin
            getVinFromSheet(sheet1);
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(), e);
            return;
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return;
        }

    }

    private static void getVinFromSheet(Sheet sheet) throws InterruptedException {

        int importSize = sheet.getLastRowNum();
        if (importSize <= 1) {
            return;
        }
        for (int i = 0; i < importSize; i++) {
            Row row = sheet.getRow(i+1);
            if (row != null) {
                Cell cell0 = row.getCell(0);
                if (cell0 == null) {
                    continue;
                }
                String vin = cell0.toString().trim();
                System.out.println(vin);
//                BlockQueueManager.putVin2Queue(vin);
            }
        }
        return;
    }

    public static void main(String[] args) {
        getTBoxDataFromExcel();
//        int size = BlockQueueManager.vinBlockQueue.size();
//        System.out.println(size);
    }
}
